package com.yanfan.ccapicode.controller;

import cn.hutool.core.collection.CollUtil;
import com.yanfan.ccapicode.enums.ThirdPartyCloudEnum;
import com.yanfan.ccapicode.service.ThirdPartyPortalService;
import com.yanfan.common.annotation.Anonymous;
import com.yanfan.common.core.domain.AjaxResult;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;

/**
 * :)
 * */
@Slf4j
@Anonymous
@RestController
@RequestMapping("/portal")
public class PortalController {

    @Autowired
    private ThirdPartyPortalService thirdPartyPortalService;

    @RequestMapping("google")
    public String google(@RequestBody String payload, HttpServletRequest request) {
        test(request);
        return thirdPartyPortalService.portal(payload, ThirdPartyCloudEnum.GOOGLE);
    }

    @RequestMapping("alexa")
    public String alexa(@RequestBody String payload, HttpServletRequest request) {
        test(request);
        return thirdPartyPortalService.portal(payload, ThirdPartyCloudEnum.ALEXA);
    }

    /**
     * {
     *     "header": {
     *         "payloadVersion": "1",
     *         "name": "DiscoverAppliancesRequest",
     *         "namespace": "DuerOS.ConnectedHome.Discovery",
     *         "messageId": "2fe901ae37ef400a9562c833f7c46a46_DCS-10-141-201-209-2016-0628194811-23619647_0#2_0_Smarthome_685fd67bf09b70.95998875_477dc6ca"
     *     },
     *     "payload": {
     *         "accessToken": "821d1671-26a0-452b-81be-d7e4cf21c743",
     *         "openUid": "d3eadfdec1c2f4c000f769ac9257ef8a"
     *     }
     * }
     * */
    @RequestMapping("baidu")
    public String baidu(@RequestBody String payload, HttpServletRequest request) {
        test(request);
        return thirdPartyPortalService.portal(payload, ThirdPartyCloudEnum.BAIDU);
    }

    @RequestMapping("xiaomi")
    public String xiaomi(@RequestBody String payload, HttpServletRequest request) {
        test(request);
        return thirdPartyPortalService.portal(payload, ThirdPartyCloudEnum.XIAOMI);
    }



    @SneakyThrows
    @RequestMapping("test")
    public AjaxResult test(HttpServletRequest request) {
        Map<String, String[]> params = request.getParameterMap();
        log.info("【自定义技能测试】测试 params: {}", params);
        if (!CollUtil.isEmpty(params)) {
            params.keySet().forEach(key -> {
                log.info("【自定义技能测试】 " + key + ": " + Arrays.toString(params.get(key)));
            });
        } else {
            log.info("【自定义技能测试】 params为空");
        }

        BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
        StringBuilder responseStrBuilder = new StringBuilder();
        String inputStr;
        while ((inputStr = streamReader.readLine()) != null)
            responseStrBuilder.append(inputStr);

        log.info("【自定义技能测试】 responseStrBuilder: {}", responseStrBuilder);
        return AjaxResult.success();
    }

}
